/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.openstack.api.nova.impl.FlavorsImpl;
import com.lczyfz.istep.modules.openstack.api.nova.model.Flavor;
import com.lczyfz.istep.modules.scene.entity.SceneVmType;
import com.lczyfz.istep.modules.scene.service.SceneVmTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 云主机类型管理Controller
 * @author dg
 * @version 2017-04-07
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneVmType")
public class SceneVmTypeController extends BaseController {

	@Autowired
	private SceneVmTypeService sceneVmTypeService;
	
	@ModelAttribute
	public SceneVmType get(@RequestParam(required=false) String id) {
		SceneVmType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneVmTypeService.get(id);
		}
		if (entity == null){
			entity = new SceneVmType();
		}
		return entity;
	}
	
	@RequiresPermissions("scene:sceneVmType:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneVmType sceneVmType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SceneVmType> page = sceneVmTypeService.findPage(new Page<SceneVmType>(request, response), sceneVmType); 
		model.addAttribute("page", page);
		return "modules/scene/sceneVmTypeList";
	}

	@RequiresPermissions("scene:sceneVmType:view")
	@RequestMapping(value = "form")
	public String form(SceneVmType sceneVmType, Model model) {
		model.addAttribute("sceneVmType", sceneVmType);
		return "modules/scene/sceneVmTypeForm";
	}

	@RequiresPermissions("scene:sceneVmType:edit")
	@RequestMapping(value = "save")
	public String save(SceneVmType sceneVmType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sceneVmType)){
			return form(sceneVmType, model);
		}
		logger.debug("Flavor=== [{}]",sceneVmType.getName()+" "+sceneVmType.getRam()+" "+sceneVmType.getVcpus()+" "+" "+sceneVmType.getDisk()+" ");
		if(sceneVmTypeService.get(sceneVmType)==null) {
			logger.debug("插入！");
			try{
				FlavorsImpl flavorImpl=new FlavorsImpl();
				Flavor flavor =new Flavor();
				flavor.setFlavorName(sceneVmType.getName());
				flavor.setFlavorRam(sceneVmType.getRam());
				flavor.setFlavorVcpus(sceneVmType.getVcpus());
				flavor.setFlavorDisk(sceneVmType.getDisk());
				logger.debug("Flavor=== [{}]",sceneVmType.getName()+" "+sceneVmType.getRam()+" "+sceneVmType.getVcpus()+" "+" "+sceneVmType.getDisk()+" ");

				Flavor reFlavor=flavorImpl.createFlavor(flavor);

				sceneVmType.setId(reFlavor.getFlavorId());
				sceneVmTypeService.preInsert(sceneVmType);
				sceneVmTypeService.insert(sceneVmType);
				addMessage(redirectAttributes, "保存云主机类型成功");
			}catch (RuntimeException e){
				addMessage(redirectAttributes, "保存云主机类型失败");
			}

		}else{
			logger.debug("修改！");
		}

		return "redirect:"+Global.getAdminPath()+"/scene/sceneVmType/?repage";
	}
	
	@RequiresPermissions("scene:sceneVmType:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneVmType sceneVmType, RedirectAttributes redirectAttributes) {
		try{
			FlavorsImpl flavorImpl=new FlavorsImpl();
			flavorImpl.deleteFlavor(sceneVmType.getId());
			sceneVmTypeService.delete(sceneVmType);
			addMessage(redirectAttributes, "删除云主机类型成功");
		}catch (RuntimeException e){
			addMessage(redirectAttributes, "删除云主机类型失败");
		}

		return "redirect:"+Global.getAdminPath()+"/scene/sceneVmType/?repage";
	}

}